import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form
import CustomDialog from "@/components/HntManage/HntMixProportion/engineeringLocation/index.vue";
import CustomDialogPerson from "@/components/commonPublic/selectPerson/index.vue";


import {
    getConcreteRecipeByFid, //详情


    saveConcreteRecipe, //保存
    getBaseDataitemDetailByItemCode, //数据字典
    getSpecificationDataList, //材料种类名称/规格
    getPurveyorDataList, //厂商

} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "modelType"
    ],
    data() {
        return {
            activeNames: ["0", "1"],
            fileList: [],
            disabled: false, //表格中间是否禁用
            isEdit: false,
            handNum: false, //闭合申请单
            firstNum: false, //一级审核
            secondNum: false, //二级审核
            visible: "",
            title: null,
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj, //基础信息
            formObjText: formJs.formObjText,
            fSourcefileList: [],
            formData: {
                fId: null, // 配合比fid
                userId: "",
                userName: "",
                fCompanyid: null, // 所属标段id
                fCompanyname: null, // 所属标段名称
                fProjectid: null, // 所属项目id
                fProjectname: null, // 所属项目名称
                fSlumplower: null, // 要求塌落度下限
                fStartdates: "", // 开盘时间
                fPlancalculation: null, // 计划用量
                fDesigncalculation: null, // 设计用量
                fEnginerringname: null, // 工程名称
                fEnginerringpart: null, // 工程部位
                fStrengthlevel: null, // 混凝土强度等级
                fProducetasknumber: null, // 生产任务单编号
                fRecipenumber: null, // 配合比通知单编号
                fNoticedates: this.common.day(0), // 日期[]
                fAppointstationtesterid: "", // 指定驻拌合站实验人id
                fAppointstationtestername: null, // 指定驻拌合站实验人名称
                fFillingdates: this.common.day(0), // 填报日期
                fInformantid: null, // 填报人id
                fInformantname: null, // 填报人名称
                fMaterialdetailid: null, // 子表id
                fSlumpupper: null, // 要求塌落度上限
                fFillingfile: null, // 填报时附件资料id
                recordfiles: null, // 信息相关文件列表
                materialList: [
                    ["基准配合比(Kg/m³)", 1, 22, 3, 4, 5, 6, 7, 8, 9, 10],
                    ["含水率(%)", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                    ["生产配合比(Kg/m³)", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                ],
                fStationfile: null, // 场站实测时附件资料id



            },
            tableData: [],
            tableObj: [],
            materialObj: [], //材料集合
            specificationObj: [], //规格型号
            suppliernameObj: [], //生产厂商

            baseFormData: {},
            closeFormObj: formJs.closeFormObj, //闭合


            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件

            btnShow: true,
            currentDetail: null,


            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null,
            engineeringLocationRow: {},
            engineeringLocationShow: false,

            personShow: false,
            selectArr:[]

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialogPerson
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        modelType: {
            handler(newVal) {
                if (newVal) {
                    this.modelTypes = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {


                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    // fStatus

                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) // currentRow
                    }
                }
            },

            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {

                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));

        this.btnShow = true
        // this.getBaseDataitemDetailByItemCode('Hnttype', 1) //混凝土种类(Hnttype)
        // this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
        // this.getBaseDataitemDetailByItemCode('Sgsite', 3) //混凝土施工部位(Sgsite)
        this.handNum = true; //闭合申请单
        this.firstNum = true; //一级审核
        if (this.statusNum == 1) { //新增
            this.getDetail({
                fId: ""
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.disabled = false;
            this.formObjText.map((item) => {
                item.disabled = false;
            });
            this.formObjText[0].disabled = true;
            this.formObjText[3].uploadSelect = true
            this.formObj[5].inputButton = true;
            this.formObjText[2].inputButton = true;

            this.isEdit = false
        } else if (this.statusNum == 2) { //编辑
            this.disabled = false;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[5].inputButton = true;

            this.formObjText.map((item) => {
                item.disabled = false;
            });
            this.formObjText[2].inputButton = true;

            this.formObjText[0].disabled = true;
            // this.formObj[3].disabled = true;
            this.formObjText[3].uploadSelect = true
            this.isEdit = false

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.disabled = true;
            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[5].inputButton = false;

            this.formObjText.map((item) => {
                item.disabled = true;
            });
            this.isEdit = true
            this.formObjText[2].inputButton = false;

            this.formObjText[3].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        // 选择工程部位
        refreshList() {},
        engineeringLocationDialog() {
            this.engineeringLocationShow = false;
        },
        getEngineeringLocation(data) { //获取材料信息
            this.formData.fEnginerringpart = data.F_TreeName; // 报验工程部位
        },
        // 选择工程部位
        // 选择人
        personDialog() {
            this.personShow = false;
        },
        getperson(data) {//单选
          
            this.selectArr=data;
            this.formData.fAppointstationtesterid = data.fUserid;
            this.formData.fAppointstationtestername = data.fRealname;
        },
        // 选择人
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },

        
        clickDialog(index, prop) {
            console.log(index, prop);
            if (index == 5 && prop == "fEnginerringpart") {
                this.engineeringLocationShow = true;
                // this.getDataList();
            } else if (index == 2 && prop == "fAppointstationtestername") {
                this.personShow = true;
              
                // this.formData.fAppointstationtesterid = this.newData.fUserid;
                // this.formData.fAppointstationtestername = this.newData.fRealname;
                //   this.candidate = "选择驻拌合站试验人";

                //   this.getBaseCompanyList();
            }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },

        //材料选择
        selectMaterial(row, name, index, item) { //存放位置选择
            this.formData.yjQmpGyLlphbrawmaterial[index].fMaterialname = item.fMaterialname;
            this.formData.yjQmpGyLlphbrawmaterial[index].fMaterialid = item.fMaterialid;

            this.tableObj[index].specifications = [];
            this.formData.yjQmpGyLlphbrawmaterial[index].fSpecification = [];

            // this.tableObj.map(item => {
            //     item['specifications'] = [];
            //     item.currentRow.map(i => {
            //         if (i.fMaterialname == name.fMaterialname) {
            //             item['specifications'].push(i)
            //         }
            //     })
            // })
            row.currentRow.map(v => {
                if (v.fMaterialname == name.fMaterialname) {
                    this.tableObj[index].specifications.push(v);
                }
            })

            this.$forceUpdate()
            this.tableData = this.tableObj;
        },
        // 规格型号选择
        selectSpecification(row, name, index, item) { //存放位置选择
            this.formData.yjQmpGyLlphbrawmaterial[index].fSpecification = item.fVersion
        },
        // 厂商
        selectPurveyor(row, name, index, item) { //存放位置选择

            this.formData.yjQmpGyLlphbrawmaterial[index].fSuppliername = item.fMerchantname
            this.formData.yjQmpGyLlphbrawmaterial[index].fSupplierid = item.fId
        },
        // 数据字典
        getBaseDataitemDetailByItemCode(status, num) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {

                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                if (num == 1) { //混凝土种类
                    this.formObj[4].options = arr
                } else if (num == 2) { //设计强度等级
                    this.formObj[5].options = arr

                } else if (num == 3) { //混凝土施工部位
                    this.formObj[6].options = arr

                }
            })
        },
        // 去重
        fn3(tempArr) {
            let result = [];
            let obj = {};
            if (tempArr != null) {
                for (let i = 0; i < tempArr.length; i++) {
                    if (!obj[tempArr[i].fMaterialname]) {
                        result.push(tempArr[i]);
                        obj[tempArr[i].fMaterialname] = true;
                    }
                }
                return result;
            }

        },
        gets(data, id, index) {
            var obj = {
                page: 1,
                pageSize: 500,
                name: "", //材料种类/名称
                fVersion: "", //规格型号
                fId: id
            }
            this.tableObj[index]['specifications'] = []
            // this.loading = true;
            var that = this;
            getSpecificationDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        data.map(item => { //大表

                            if (res.data.pageInfo.list.length > 0) {
                                res.data.pageInfo.list.map(i => {
                                    if (item.fMaterialname == i.fMaterialname) {
                                        this.tableObj[index]['specifications'].push(i)
                                    }
                                })
                            }
                        })

                        that.$forceUpdate()
                        that.tableData = this.tableObj


                    } else {

                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错1", error.msg);
            //     this.loading = false;
            // });


        },
        // 材料名称
        getSpecificationDataLists(pageSize, id, index, status) {

            var obj = {
                page: 1,
                pageSize: 500,
                name: "", //材料种类/名称
                fVersion: "", //规格型号
                fId: id
            }
            // this.loading = true;
            var that = this;
            getSpecificationDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        this.tableObj[index]['specificationData'] = [];
                        this.tableObj[index]['specificationData'] = this.fn3(res.data.pageInfo.list);
                        this.tableObj[index]['currentRow'] = res.data.pageInfo.list;
                        that.$forceUpdate()

                        if (status == 0) {
                            this.gets(this.tableObj, id, index)
                        } else {
                            that.tableData = this.tableObj
                        }

                        that.getPurveyorDataList(id, index)

                    } else {

                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错2", error.msg);
            //     this.loading = false;
            // });


        },
        // 厂商 getPurveyorDataList
        getPurveyorDataList(id, index) {
            var obj = {
                page: 1,
                pageSize: 500,
                merchantName: "", //生产厂家
                typ: "1", //"1"材料生产商，"2"材料供应商
                fVarietyid: id
            }
            // this.loading = true;
            // var that = this;
            getPurveyorDataList(obj)
                .then((res) => {

                    if (res.errcode == 0) {

                        this.tableObj[index]['PurveyorData'] = []
                        this.tableObj[index]['PurveyorData'] = res.data.pageInfo.list;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错3", error.msg);
            //     this.loading = false;
            // });
        },
        // 详情
        getDetail(obj) {
            // var that = this
            getConcreteRecipeByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.concreteRecipe
                    // this.formData = data;
                    if (obj.fId == '') {
                        this.formData.fFillingfile = data.fFillingfile;
                        this.formData.fStationfile = data.fStationfile;
                        this.formData.materialList = data.materialList;
                        this.formData.fInformantname = this.userInfo.realName
                        this.formData.fCompanyid = this.userInfo.companyId //标段id
                        this.formData.fCompanyname = this.userInfo.companyName //标段名称
                        this.formData.fProjectid = this.userInfo.projectId //项目id
                        this.formData.fProjectname = this.userInfo.projectName //项目名称
                        this.formData.fReportid = this.userInfo.userId; //当前登录人id
                        this.formData.fReportname = this.userInfo.realName; //当前登录人名称
                        this.formData.userId = this.userInfo.userId; //当前登录人id
                        this.formData.userName = this.userInfo.realName; //当前登录人名称
                    } else {
                        this.formData = res.data.concreteRecipe;
                        this.fSourcefileList = res.data.fillingfile;
                    }



                }
            });
        },
        yj_saveForm() {


            console.log(this.formData);
            if (this.$refs.publicForm.submitForm() && this.$refs.publicForm0.submitForm()) {

                this.saveForm(this.formData, 0);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(obj) {

            this.isLock = true
            saveConcreteRecipe(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};